/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Hamish Morgan
 */
public class ClientWindow extends JFrame {

    private static final ResourceBundle strings = ResourceBundle.getBundle(
            "strings", Locale.getDefault());

    private final JFrame fullscreenFrame;
    private boolean fullscreen;
    private ClientConfig config;

    public ClientWindow(ClientConfig cfg) {
        this.config = cfg;
        setLayout(new BorderLayout());
        setSize(config.getWidth(), config.getHeight());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(strings.getString("app_name"));
        getAccessibleContext().setAccessibleDescription(
                strings.getString("app_description"));

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation(d.width / 2 - config.getWidth() / 2, d.height / 2 - config.getHeight() / 2);

        fullscreenFrame = new JFrame();
        fullscreenFrame.setLayout(new BorderLayout());
        fullscreenFrame.setUndecorated(true);
        fullscreenFrame.setResizable(false);
        fullscreenFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        fullscreenFrame.setIgnoreRepaint(true);
        fullscreen = false;

    }

    @Override
    public Component add(Component comp) {
        if (comp == super.getComponent(0))
            return comp;
        JFrame frame = fullscreen ? fullscreenFrame : this;
        frame.getContentPane().removeAll();
        frame.getContentPane().add(comp, BorderLayout.CENTER);

        comp.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_F) {
                    setFullscreen(!isFullScreen());
                }
            }
        });

        return super.add(comp);
    }

    public boolean isFullScreen() {
        return fullscreen;
    }

    public void setFullscreen(boolean fullscreen) {

        if (this.fullscreen == fullscreen)
            return;

        final GraphicsDevice d = this.getGraphicsConfiguration().getDevice();
        if (!d.isFullScreenSupported()) {
            LoggerFactory.getLogger(ClientWindow.class).warn(
                    "Fullscreen not supported.");
        }

        this.fullscreen = fullscreen;


        if (this.fullscreen) {
            fullscreenFrame.setContentPane(this.getContentPane());
            this.setVisible(false);
            fullscreenFrame.setVisible(true);
            d.setFullScreenWindow(fullscreenFrame);

        } else {
            d.setFullScreenWindow(null);
            fullscreenFrame.setVisible(false);
            this.setContentPane(fullscreenFrame.getContentPane());

            this.setVisible(true);
//            this.invalidate();
//            this.validateTree();
//            this.repaint();
            this.setBounds(getX(), getY(), getWidth()-1, getHeight());
        }

    }
}
